#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_cecdat_kamailio_NativeLib_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

#include "core/dprint.h"
int main(int argc, char** argv);

extern "C"
JNIEXPORT void JNICALL
Java_com_cecdat_kamailio_NativeLib_Start(JNIEnv *env, jobject thiz, jstring configFile, jstring nativeLibraryDir, jstring dataDir, jstring address) {
    const char* cfg_file = env->GetStringUTFChars(configFile, NULL);
    const char* modules_search_path = env->GetStringUTFChars(nativeLibraryDir, NULL);
    const char* data_dir = env->GetStringUTFChars(dataDir, NULL);
    const char* ip_address = env->GetStringUTFChars(address, NULL);

    char runtime_dir[260] = {0};
    sprintf(runtime_dir, "%s/kamailio", data_dir);

    const char* argv[30];
    argv[0] = "./kamailio";
    argv[1] = "-f";                 // -f file      Configuration file
    argv[2] = cfg_file;
    argv[3] = "-E";                 // -E           Log to stderr
    argv[4] = "-L";                 // -L path      Modules search path
    argv[5] = modules_search_path;
    argv[6] = "-Y";                 // -Y dir       Runtime dir path
    argv[7] = runtime_dir;
    argv[8] = "-D";                 // -D..do not fork (almost) anyway;
    argv[9] = "-l";                 // Listen on the specified address/interface
    argv[10] = ip_address;
    int argc = 11;

    LOGI("cfg_file: %s", cfg_file);
    LOGI("modules_search_path: %s", modules_search_path);
    LOGI("runtime_dir: %s", runtime_dir);

    main(argc, (char**)argv);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_cecdat_kamailio_NativeLib_Stop(JNIEnv *env, jobject thiz) {
    // TODO: implement Stop()
}
